import { Application } from '../../typings/app';
import { getDefaultFields } from '../utils';

/* indent size: 1 */
export default ({ model, Sequelize: { DataTypes } }: Application) => {
	const CategoryModel = model.define('category', {
		id: {
			type: DataTypes.INTEGER.UNSIGNED,
			allowNull: false,
			primaryKey: true,
			autoIncrement: true
		},
		name: {
			type: DataTypes.STRING(50),
			allowNull: false,
			defaultValue: ''
		},
		desc: {
			type: DataTypes.STRING(500),
			allowNull: true,
			defaultValue: ''
		},
		pid: {
			type: DataTypes.INTEGER.UNSIGNED,
			allowNull: true,
			defaultValue: null
		},
		level: {
			type: DataTypes.TINYINT.UNSIGNED,
			allowNull: false,
			defaultValue: 0
		},
		type: {
			type: DataTypes.TINYINT,
			allowNull: false,
			defaultValue: 0,
            comment: '0 -> 后台， 1 -> 前台'
		},
		...getDefaultFields()
	}, {
		tableName: 'category',
		timestamps: false
	});


	CategoryModel.sync({ alter: true });
	//@ts-ignore
	CategoryModel.associate = function () {
		//@ts-ignore
		model.Category.hasOne(model.Category, {
			foreignKey: 'pid',
			constraints: false,
			sourceKey: 'id',
			as: 'parent'
		});
	}
	return CategoryModel
}